package com.zhu.ttwords.view.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhu.ttwords.R;
import com.zhu.ttwords.bean.WordBean;

import java.util.Calendar;

/**
 * Created by KnightYe on 2015/4/4.
 * StudyView,independent View with some study method.
 */
public class StudyView extends FrameLayout implements View.OnLongClickListener, View.OnFocusChangeListener {
    //单词预览一共有一下几种状态
    public static final int STATUS_INIT = 0x00;//初始化时候，exam不显示，context显示，prono显示，mark不显示，exam可编辑
    public static final int STATUS_ATTATION = 0x10;//学习答题时，exam不显示，context显示，prono显示，mark显示，exam可编辑
    public static final int STATUS_WRONG = 0x20;//学习答题结束时，exam不显示，context显示，prono显示，mark显示，exam可编辑
    public static final int STATUS_RIGHT = 0x30;//学习答题结束时，exam不显示，context显示，prono显示，mark显示，exam可编辑
    WordBean bean;//存储数据的bean。
    ViewGroup group;//content和prono的集合。
    TextView explain;
    TextView pos;
    TextView prono;
    EditText exam;
    EditText content;
    ImageView mark;
    ViewGroup focus;
    OnStudyResultListener listener;
    long startTime;
    private boolean isStudayMode = true;// studay is true,exam is false;
    private boolean isExamMode = false;// studay is true,exam is false;
    private int status = 0;//状态标志位。
    private int alph = 0;

    public StudyView(final Context context) {
        super(context);
        bean = new WordBean();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(R.layout.view_study, this);
        group = (ViewGroup) rootView.findViewById(R.id.view_study_down);
        explain = (TextView) rootView.findViewById(R.id.view_study_explain);
        pos = (TextView) rootView.findViewById(R.id.view_study_pos);
        prono = (TextView) rootView.findViewById(R.id.view_study_prono);
        exam = (EditText) rootView.findViewById(R.id.view_study_test);
        content = (EditText) rootView.findViewById(R.id.view_study_content);
        mark = (ImageView) rootView.findViewById(R.id.view_study_mark);
        focus = (ViewGroup) rootView.findViewById(R.id.view_study_focus);
        content.setOnFocusChangeListener(this);
        exam.setOnFocusChangeListener(this);
        mark.setOnLongClickListener(this);
    }

    public StudyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StudyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public StudyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public WordBean getBean() {
        return bean;
    }

    /**
     * 设置数据方法。重要。学习状态无输出。
     *
     * @param bean 显示bean数据。
     */
    public void setBean(WordBean bean) {
        this.bean = bean;
        prono.setText(bean.getWprono());
        explain.setText(bean.getWexplain());
        pos.setText(bean.getWpos());
        content.setText(bean.getWcontent());
        content.setHint(bean.getWcontent());
        exam.setHint(bean.getWcontent());
        setStatus(status);
    }

    /**
     * 复习模式监听器
     *
     * @param listener 实现了OnStudyResultListener接口的类。
     */
    public void setOnExamSaveListener(OnStudyResultListener listener) {
        this.listener = listener;
    }

    public void setExamMode() {
        this.isExamMode = true;
        this.isStudayMode = false;
        setStatus(STATUS_INIT);
    }

    public void setStudayMode() {
        this.isStudayMode = true;
        this.isExamMode = false;
        setStatus(STATUS_INIT);
    }


    public void setAlph(int alph) {
        this.alph = alph;
    }

    private void setStatus(int status) {
        switch (status) {
            case STATUS_INIT:
                group.setVisibility(isStudayMode ? View.VISIBLE : View.INVISIBLE);
                content.setText(bean.getWcontent());
                exam.setVisibility(isStudayMode ? View.INVISIBLE : View.VISIBLE);
                exam.getEditableText().clear();
                mark.setVisibility(View.INVISIBLE);
                mark.setImageResource(R.mipmap.img_mark_attention);
                break;
            case STATUS_ATTATION:

                group.setVisibility(isStudayMode ? View.VISIBLE : View.INVISIBLE);
                content.getEditableText().clear();
                exam.setVisibility(isStudayMode ? View.INVISIBLE : View.VISIBLE);
                exam.getEditableText().clear();
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_attention);
                if (!isStudayMode) {
                    exam.requestFocus();
//                    exam.postDelayed(
//                            new Runnable() {//等绘制完毕后获取焦点。
//                                @Override
//                                public void run() {
//                                    exam.requestFocus(0);
//                                }
//                            }, 300);//300毫秒后发送。
                }
                break;
            case STATUS_RIGHT:
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_right);
                if (isStudayMode) {
                    isStudayMode = false;
                }
                if (isExamMode) {
                    exam.setFocusable(false);
                }
                if (isExamMode && listener != null) {
                    long endTime = Calendar.getInstance().getTimeInMillis();
                    long dur = endTime - startTime;
                    if (dur < 5000) {
                        listener.saveExamResult(bean, true, 5);
                    } else if (dur < 15000) {
                        listener.saveExamResult(bean, true, 4);
                    } else {
                        listener.saveExamResult(bean, true, 3);
                    }
                }
                break;
            case STATUS_WRONG:
                mark.setVisibility(View.VISIBLE);
                mark.setImageResource(R.mipmap.img_mark_wrong);
                if (isExamMode) {
                    exam.setFocusable(false);
                }
                if (isExamMode && listener != null) {
                    long endTime = Calendar.getInstance().getTimeInMillis();
                    long dur = endTime - startTime;
                    if (dur < 5000) {
                        listener.saveExamResult(bean, false, 2);
                    } else if (dur < 15000) {
                        listener.saveExamResult(bean, false, 1);
                    } else {
                        listener.saveExamResult(bean, false, 0);
                    }
                }
                break;
            default:
                break;
        }
        this.status = status;
    }

    private void checkWord(CharSequence a) {
        if (a.toString().equals(bean.getWcontent())) {
            setStatus(STATUS_RIGHT);
        } else {
            setStatus(STATUS_WRONG);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (!isExamMode) {
            isStudayMode = true;
            setStatus(STATUS_ATTATION);
            content.requestFocus();
        }
        return true;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.view_study_content:
                if (hasFocus) {//获得焦点-答题 or 切换到测试模式
                    setStatus(STATUS_ATTATION);
                } else {//离开焦点-交卷
                    checkWord(isStudayMode ? content.getText() : exam.getText());
                }
                break;
            case R.id.view_study_test:
                if (hasFocus) {//答题
                    startTime = Calendar.getInstance().getTimeInMillis();//开始计时
                    setStatus(STATUS_ATTATION);
                } else {//交卷
                    checkWord(isStudayMode ? content.getText() : exam.getText());
                }
                break;
            default:
                break;
        }
    }

    /**
     * Created by KnightYe on 2015/4/7.
     * 当Exam模式时候回答完毕回调接口
     */
    public static interface OnStudyResultListener {
        public void saveExamResult(WordBean bean, boolean flag, int level);

        public void saveStudyResult(WordBean bean, boolean flag);
    }

}
